var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.getAnnouncementById = function (msg, callback) {
    let announcementService = this.app.get("announcementService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    let announcement = announcementService.getAnnouncementById(parseInt(msg["id"]));
    if(!!announcement) {
        callback({
            resultCode: 0,
            message: "ok",
            announcement: announcement.toJSON()
        });
    }else{
        callback({
            resultCode: 404,
            message: "announcement为空.get"
        });
    }

};

Remote.prototype.getAllAnnouncements = function (msg, callback) {
    let announcementService = this.app.get("announcementService");
    callback({
        resultCode: 0,
        message: "ok",
        announcement: announcementService.toJSON()
    });
};

Remote.prototype.updateAnnouncement = function (msg, callback) {
    let announcementService = this.app.get("announcementService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("title")) {
        callback({resultCode: 404, message: "title为空"});
        return;
    }
    if(!msg.hasOwnProperty("content")) {
        callback({resultCode: 404, message: "content为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }

    let id = parseInt(msg["id"]);
    let title = msg["title"];
    let content = msg["content"];
    let startTime = parseInt(msg["startTime"]);
    let endTime = parseInt(msg["endTime"]);
    announcementService.updateAnnouncement(id, title, content, startTime, endTime, function (announcement) {
        if(!!announcement) {
            callback({
                resultCode: 0,
                message: "ok",
                announcement: announcement.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "announcement为空.update"
            });
        }
    });
};

Remote.prototype.addAnnouncement = function (msg, callback) {
    let announcementService = this.app.get("announcementService");
    if(!msg.hasOwnProperty("title")) {
        callback({resultCode: 404, message: "title为空"});
        return;
    }
    if(!msg.hasOwnProperty("content")) {
        callback({resultCode: 404, message: "content为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    let title = msg["title"];
    let content = msg["content"];
    let startTime = parseInt(msg["startTime"]);
    let endTime = parseInt(msg["endTime"]);
    announcementService.addAnnouncement(title, content, startTime, endTime, function (announcement) {
        if(!!announcement) {
            callback({
                resultCode: 0,
                message: "ok",
                announcement : announcement.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "announcement为空.add"
            });
        }
    });
};

Remote.prototype.deleteAnnouncementById = function (msg, callback) {
    let announcementService = this.app.get("announcementService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    announcementService.deleteAnnouncementById(parseInt(msg["id"]), function (id) {
        if(!!id) {
            callback({
                resultCode: 0,
                message: "ok",
                announcement : {id : id}
            });
        }else{
            callback({
                resultCode: 0,
                message: "announcement为空.delete"
            });
        }
    });
};

